﻿#include "pch.h"
#include "ObjectDetectionInfer.h"
#include "SegmentionInfer.h"
#include "ClassificationInfer.h"
#include "opencv.hpp"

using namespace cv;
using namespace dnn;

#if 0
using namespace winrt;
using namespace Windows::Foundation;
#endif


std::vector<Scalar> colors = { Scalar(255, 0, 0), Scalar(255, 0, 255), Scalar(170, 0, 255), Scalar(255, 0, 85),
								   Scalar(255, 0, 170), Scalar(85, 255, 0), Scalar(255, 170, 0), Scalar(0, 255, 0),
								   Scalar(255, 255, 0), Scalar(0, 255, 85), Scalar(170, 255, 0), Scalar(0, 85, 255),
								   Scalar(0, 255, 170), Scalar(0, 0, 255), Scalar(0, 255, 255), Scalar(85, 0, 255) };

const std::vector<std::string> class_names = {
	"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
	"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
	"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
	"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
	"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
	"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
	"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
	"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
	"hair drier", "toothbrush" };


//#define TEST_CLS
//#define TEST_OBJ
//#define TEST_SEG

int main()
{
#if 0
    init_apartment();
    Uri uri(L"http://aka.ms/cppwinrt");
    printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
#endif

	ClassificationInfer clsInfer;
	ObjectDetectionInfer obInfer;
	SegmentionInfer segInfer;

#ifdef TEST_CLS

	auto res = clsInfer.LoadModel("./DL/best.onnx", "./DL/class.txt");
	auto image = cv::imread("./DL/weld_5.jpg");
	if (!image.empty()) {
		auto clsRes = clsInfer.Infer(image);
		for (auto& r : clsRes) {
			std::cout << r.class_id << " confidence " << r.confidence << std::endl;
		}
	}
	std::cout << res << std::endl;
	system("pause");
	return 0;
#endif

#ifdef TEST_OBJ
	auto res = obInfer.LoadModel("./DL/yolov8n-seg.onnx", "./DL/class.txt");
	auto image = cv::imread("./DL/weld_5.jpg");
	if (!image.empty()) {
		auto clsRes = obInfer.Infer(image);
	}
	std::cout << res << std::endl;
	system("pause");
	return 0;	
#endif

#ifdef TEST_SEG
	auto res = segInfer.LoadModel("./DL/yolov8n-seg.onnx", "./DL/class.txt");
	auto image = cv::imread("./DL/weld_5.jpg");
	if (!image.empty()) {
		auto clsRes = segInfer.Infer(image);
	}
	std::cout << res << std::endl;
	system("pause");
	return 0;	

#endif


	try {
		obInfer.LoadModel();
		segInfer.LoadModel();

		auto image = cv::imread("./DL/2024-01-22_11-38-16.129_OK_R4.jpg");

		if (!image.empty())
		{
#if 0
			auto res = obInfer.Infer(image);
			for (auto r : res) {
				cv::rectangle(image, r.box, cv::Scalar(0, 255, 0), 2); // 

			}
			cv::imshow("Image with Box", image);
			cv::waitKey(0);
#else

			clock_t clk = std::clock();
			for (int i = 0; i < 100; i++) {
				auto res = obInfer.Infer(image);
			}
			clock_t cost = std::clock() - clk;
			double avrage = cost / 100.0;
			std::cout << "cost average  " << avrage << " ms\n";
			
#endif
		}
	}
	catch (std::exception& e)
	{
		std::cout << "error " << e.what() << std::endl;
	}
	system("pause");
}
